package com.v1.video.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.v1.video.R;
import com.v1.video.widgets.MyDialog;

public class CustomActionSheetDialog extends MyDialog{

	private Context mCxt;

	private List<String> mActions=new ArrayList<String>();
	private LayoutInflater mInflater;
	private ListView mActionLv;
	
	private OnActionSheetItemClick onActionSheetItemClick;
	
	public void setOnActionSheetItemClick(
			OnActionSheetItemClick onActionSheetItemClick) {
		this.onActionSheetItemClick = onActionSheetItemClick;
	}

	public interface OnActionSheetItemClick{
		public void onClick(int index,String actionName);
	}
	
	public void changeTitleByIndex(int index,String title){
		if(mActions.size()>1){
			mActions.set(index, title);
		}
	}

	public CustomActionSheetDialog(Context context,String[] actionStrs) {
		super(context,R.style.dialog_headsetting);
		this.mCxt = context;
		if (actionStrs == null) {
			this.mActions.add( "取消");
		} else {
			for(int i=0;i<actionStrs.length;i++){
				this.mActions.add(actionStrs[i]);
			}
			this.mActions.add( "取消");
		}
		this.mInflater=LayoutInflater.from(context);
		LinearLayout linearlayout;
		android.view.WindowManager.LayoutParams layoutparams;
		linearlayout = (LinearLayout)mInflater.inflate(R.layout.custom_action_sheet_dialog_lay, null);
		linearlayout.setMinimumWidth(10000);
		
		layoutparams = this.getWindow().getAttributes();
		layoutparams.x = 0;
		layoutparams.y = -1000;
		layoutparams.gravity = 80;//bottom
		layoutparams.alpha=1.0f;
		this.onWindowAttributesChanged(layoutparams);
		
		this.setCanceledOnTouchOutside(true);
		
		this.mActionLv=(ListView)linearlayout.findViewById(R.id.listView1);
		this.mActionLv.setAdapter(new MyAdapter());
		
		this.mActionLv.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if (CustomActionSheetDialog.this.isShowing()) {
					CustomActionSheetDialog.this.dismiss();
				}
				if (arg2 != mActions.size() - 1) {
					if (onActionSheetItemClick != null) {
						onActionSheetItemClick.onClick(arg2, mActions.get(arg2));

					}
				}				
			}});
		
		this.setContentView(linearlayout);
	}
	
	private class MyAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mActions.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return mActions.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			PlaceHolder holder;
			if(convertView == null){
				convertView=mInflater.inflate(R.layout.custom_action_sheet_dialog_lay_item, null);
				holder=new PlaceHolder();
				holder.cancelLine = convertView.findViewById(R.id.cancelLine);
				holder.actionName=(TextView)convertView.findViewById(R.id.actionName);
				holder.line = convertView.findViewById(R.id.line);
				convertView.setTag(holder);
			}else{
				holder=(PlaceHolder)convertView.getTag();
			}
			/*holder.line.setVisibility(View.GONE);
			if(position == 0)
				holder.line.setVisibility(View.VISIBLE);*/
			if(position==mActions.size() - 1){
				holder.cancelLine.setVisibility(View.VISIBLE);
				//TextPaint tp = holder.actionName.getPaint(); 
				//tp.setFakeBoldText(true); 

				//holder.actionName.setBackgroundResource(R.drawable.action_sheet_cancel_selector);
			}else{
				holder.cancelLine.setVisibility(View.GONE);
				//TextPaint tp = holder.actionName.getPaint(); 
				//tp.setFakeBoldText(false); 
				//holder.actionName.setBackgroundResource(R.drawable.action_sheet_no_cancel_selector);
			}
			holder.actionName.setText(mActions.get(position));
			return convertView;
		}
		
		private class PlaceHolder {
			private View cancelLine;
			private TextView actionName;
			private View line;
		}
		
	}
}
